﻿using System;
using System.Threading;
using System.Windows;

namespace SharePoint.Security.ContentScanner.Client
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        static Mutex mutex = new Mutex(false, "ContentScannerSingleInstance");

        protected override void OnStartup(StartupEventArgs e)
        {
            if (!mutex.WaitOne(TimeSpan.Zero, false))
            {
                MessageBox.Show("Sorry! You cannot run multiple instances of the same program. An instance of the application is already running.",
                    "Sharepoint Content Scanner - Warning", MessageBoxButton.OK, MessageBoxImage.Stop);
                Environment.Exit(1);
            }

            AppDomain.CurrentDomain.UnhandledException +=
                         new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

            base.OnStartup(e);
        }

        void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Exception ex = e.ExceptionObject as Exception;
            MessageBox.Show(ex.Message, "Sharepoint Content Scanner - Error",
                            MessageBoxButton.OK, MessageBoxImage.Error);
        }

    }
}
